/***
This do-file creates a figure comparing 7-day moving averages of national trends
in cash transactions in CoinOut data vs. card spending on groceries in Affinity 
Solutions data between January 1 2020 and June 1 2020.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/`category'"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Cash Spending in CoinOut Transactions Data vs Card Spending.yaml"

*-------------------------------------------------------------------------------
* Aggregate affinity and coinout 
*-------------------------------------------------------------------------------

project, uses("${root}/data/derived/CoinOut/coinout_daily_winsorized.dta")
use "${root}/data/derived/CoinOut/coinout_daily_winsorized.dta", clear

drop if mi(total)
rename total_winsorized_nooutliers coin_spend

gcollapse (sum) coin_spend, by(date) fast

drop if date == . 
tsset date
tsfill, full

replace coin_spend = 0 if mi(coin_spend)

gen ma = (coin_spend + F1.coin_spend + F2.coin_spend + F3.coin_spend + F4.coin_spend + F5.coin_spend + F6.coin_spend) / 7

gen temp = ma if inrange(date, td(04jan2020), td(31jan2020))
gegen mean = mean(temp)

gen normed_coinout = 100 * (ma/mean - 1)

tempfile normed_coinout
save `normed_coinout'

* Affinity
project, uses("${root}/data/web/data/Affinity - National - Daily.csv")
import delimited "${root}/data/web/data/Affinity - National - Daily.csv", clear

gen date = mdy(month, day, year)
format date %td
keep if year(date) == 2020
keep spend_s_grf date
replace spend_s_grf = spend_s_grf * 100
replace date = date - 6

merge 1:1 date using `normed_coinout'

keep if date > td(03jan2020) & date < td(01dec2020)

gen Fridays = date - dow(date) + 5
format Fridays %td
drop date 
rename Fridays date

gcollapse normed_coinout spend_s_grf, by(date)

* gen weekly changes
sort date
gen change_normed_coinout = normed_coinout - normed_coinout[_n-1]
gen change_spend_s_grf = spend_s_grf - spend_s_grf[_n-1]

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

corr change_normed_coinout change_spend_s_grf 
local corr: di %04.2f `r(rho)'
di `corr'

tw ///
	(line change_normed_coinout date, color(oi3)) ///
	(line change_spend_s_grf date, color(oi2)) ///
	, /// 
	legend(order(1 "CoinOut Total Spending" 2 "Affinity Grocery Spending")) ///
	ytitle("Week-to-Week Change (p.p.)" "in Consumer Spending") ///
	ylab(40 "+40 p.p." 20 "+20 p.p." 0 "0 p.p." -20 "-20 p.p.", nogrid) ///
	xlab(`=mdy(1, 1, 2020)' `""Jan" "2020""' `=mdy(2, 1, 2020)' "Feb" `=mdy(3, 1, 2020)' "Mar" /// 
	`=mdy(4, 1, 2020)' "Apr" `=mdy(5, 1, 2020)' "May" `=mdy(6, 1, 2020)' "Jun" ///
	`=mdy(7, 1, 2020)' "Jul" `=mdy(8, 1, 2020)' "Aug" `=mdy(9, 1, 2020)' "Sep" ///
	`=mdy(10, 1, 2020)' "Oct" `=mdy(11, 1, 2020)' "Nov" `=mdy(12, 1, 2020)' "Dec" ) ///
	text(-25 `=mdy(11, 1, 2020)' "Correlation: `corr'", size(medsmall) color(gs8)) ///
	${title_`version'} ///
	xtitle("") 
oi_graph_export "${root}/results/Spending/CoinOut vs Affinity Spending", type(${fig_type})

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

yamlout using "${root}/results/paper numbers/`category'/Cash Spending in CoinOut Transactions Data vs Card Spending.yaml", ///
	key("aff_coinout_corr") ///
	comment("Correlation Affinity Grocery vs Coinout") ///
	value(`corr') fmt(%04.2f)

project, creates("${root}/results/paper numbers/`category'/Cash Spending in CoinOut Transactions Data vs Card Spending.yaml")
